//
//  AceListCell.m
//  APP_login
//
//  Created by edz on 7/28/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "AceListCell.h"
#import "Masonry.h"
//#import "UIImageView+Webcaech"

@interface AceListCell ()
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *nickNameLable;
@property (nonatomic, strong) UILabel *describeLable;
@property (nonatomic, strong) UILabel *introduceLable;
@property (nonatomic, strong) UIView *cellContentView;
@property (nonatomic, strong) UIButton *attentionButton;

@property (nonatomic, strong) UILabel *diaryTitleLable;
@property (nonatomic, strong) UIImageView *contentImageView;
@property (nonatomic, strong) UILabel *groupNameLable;
@property (nonatomic, strong) UILabel *groupIntorduceLable;

@property (nonatomic, strong) UILabel *learnRecordDateLable;
@property (nonatomic, strong) UIView *firstLearnRecordView;
@property (nonatomic, strong) UIView *secondLearnRecordView;
@property (nonatomic, strong) UILabel *ellipsisLabe;
@end


@implementation AceListCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _iconView = [[UIImageView alloc] init];
        [self.contentView addSubview: _iconView];
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(40);
            make.left.mas_equalTo(20);
            make.size.mas_equalTo(CGSizeMake(56, 56));
        }];
        
        _nickNameLable = [UILabel new];
        [self.contentView addSubview: _nickNameLable];
        [_nickNameLable mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(40);
            make.left.mas_equalTo(90);
            make.size.mas_equalTo(CGSizeMake(120, 20));
        }];
        
        _describeLable = [UILabel new];
        [self.contentView addSubview: _describeLable];
        [_describeLable mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(70);
            make.left.mas_equalTo(90);
            make.size.mas_equalTo(CGSizeMake(200, 15));
        }];
        _describeLable.font = [UIFont systemFontOfSize:14];
        
        _introduceLable = [UILabel new];
        [self.contentView addSubview: _introduceLable];
        [_introduceLable mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(95);
            make.left.mas_equalTo(90);
            make.width.mas_equalTo(300);
        }];
        _introduceLable.numberOfLines = 0;
        _introduceLable.textColor = [UIColor colorWithRed:81/255.0f green:145/255.0f blue:236/255.0f alpha:1.0f];
        _introduceLable.font = [UIFont systemFontOfSize:14];
        
        _cellContentView = [UIView new];
        [self.contentView addSubview: _cellContentView];
        [_cellContentView mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(self.introduceLable.mas_bottom).offset(10);
            make.left.mas_equalTo(90);
            make.right.mas_equalTo(-15);
            make.bottom.mas_equalTo(-25);
        }];
        _cellContentView.layer.cornerRadius = 8;
        _cellContentView.layer.masksToBounds = YES;
        _cellContentView.backgroundColor = [UIColor colorWithRed:245/255.0f green:247/255.0f blue:249/255.0f alpha:1.0f];
        
        _attentionButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview: _attentionButton];
        [_attentionButton setTitle:@"+关注" forState:UIControlStateNormal];
        _attentionButton.backgroundColor = [UIColor colorWithRed:81/255.0f green:146/255.0f blue:236/255.0f alpha:1.0f];
        [_attentionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_attentionButton addTarget:self action:@selector(attentionButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_attentionButton.layer setCornerRadius:10];
        [_attentionButton.layer setMasksToBounds:YES];
        [_attentionButton mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(40);
            make.right.mas_equalTo(-20);
            make.size.mas_equalTo(CGSizeMake(60, 30));
        }];
    }
    return self;
}

- (void) setModel:(AceCellModel *)model{
//    [self.iconView setImage:[UIImage imageNamed:model.iconImage]];
    self.nickNameLable.text = model.userInfo.nickname;
    self.describeLable.text = model.userInfo.categoryAlias;
    self.introduceLable.text = model.topText;
    [self loadContentView:model];
}

- (void) loadContentView:(AceCellModel *)model{
//    if ([model.cellType isEqualToString:@"diary"]) {
//        [self loadDiaryTitleLable];
//        self.diaryTitleLable.text = model.diaryTitle;
//        [self setImageView: model.diaryArray];
//    }
//
//    if ([model.cellType isEqualToString:@"group"]) {
//        [self loadGroupIconImage];
////        [_contentImageView setImage:[UIImage imageNamed:[model.groupDict objectForKey:@"groupIcon"]]];
//
//        [self loadGroupNameLable];
////        _groupNameLable.text = [model.groupDict objectForKey:@"groupName"];
//
//        [self loadGroupIntorduceLable];
////       _groupIntorduceLable.text = [model.groupDict objectForKey:@"groupIntroduce"];
//    }
    
//    if ([model.cellType isEqualToString:@"learnRecord"]) {
//        [self loadLearnRecordDateLable];
//        _learnRecordDateLable.text = [model.learnRecordDict objectForKey:@"date"];
//
//        NSString *name = [model.learnRecordDict objectForKey:@"firstRecordName"];
//        NSString *time = [model.learnRecordDict objectForKey:@"firstRecordTime"];
//        _firstLearnRecordView = [self loadLearnRecordView:name :time];
//        [_cellContentView addSubview: _firstLearnRecordView];
//        [_firstLearnRecordView mas_makeConstraints:^(MASConstraintMaker *make){
//            make.top.mas_equalTo(self.learnRecordDateLable.mas_bottom).offset(10);
//            make.left.mas_equalTo(30);
//            make.right.mas_equalTo(-20);
//            make.height.mas_equalTo(30);
//        }];
//
//        name = [model.learnRecordDict objectForKey:@"secondRecordName"];
//        time = [model.learnRecordDict objectForKey:@"secondRecordTime"];
//        _secondLearnRecordView = [self loadLearnRecordView:name :time];
//        [_cellContentView addSubview: _secondLearnRecordView];
//        [_secondLearnRecordView mas_makeConstraints:^(MASConstraintMaker *make){
//            make.top.mas_equalTo(self.firstLearnRecordView.mas_bottom).offset(10);
//            make.left.mas_equalTo(30);
//            make.right.mas_equalTo(-20);
//            make.height.mas_equalTo(30);
//        }];
//
//        _ellipsisLabe = [UILabel new];
//        [_cellContentView addSubview:_ellipsisLabe];
//        [_ellipsisLabe mas_makeConstraints:^(MASConstraintMaker *make){
//            make.top.mas_equalTo(self.secondLearnRecordView.mas_bottom).offset(5);
//            make.left.mas_equalTo(30);
//            make.width.mas_equalTo(30);
//            make.bottom.mas_equalTo(-20);
//        }];
//        _ellipsisLabe.text = @"...";
//    }
}

-(void) setImageView:(NSArray *)array{
    if (array.count == 1) {
        _contentImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:array[0]]];
        [_cellContentView addSubview: _contentImageView];
        [_contentImageView mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(45);
            make.left.mas_equalTo(10);
            make.right.bottom.mas_equalTo(-10);
            make.height.mas_equalTo(130);
        }];
        _contentImageView.layer.cornerRadius = 8;
        _contentImageView.layer.masksToBounds = YES;
    }
    if (array.count >1) {
        NSMutableArray *imageViews = [NSMutableArray array];
        for (int i = 0 ; i< array.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:array[i]]];
            imageView.layer.cornerRadius = 8;
            imageView.layer.masksToBounds = YES;
            [self.cellContentView addSubview:imageView];
            UIImageView *image2;
            if (i != 0) {
                image2 = imageViews[i-1];
            }
            [imageView mas_makeConstraints:^(MASConstraintMaker *make){
                make.top.mas_equalTo(45);
                make.bottom.mas_equalTo(-10);
                make.size.mas_equalTo(CGSizeMake(90, 80));
                if (i == 0) {
                    make.left.mas_equalTo(10);
                }else{
                    make.left.mas_equalTo(image2.mas_right).offset(5);
                }
            }];
            [imageViews addObject:imageView];
        }
    }
}


-(void)loadDiaryTitleLable{
    if (_diaryTitleLable == nil) {
        _diaryTitleLable = [UILabel new];
        [_cellContentView addSubview:_diaryTitleLable];
        [_diaryTitleLable mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.left.mas_equalTo(15);
            make.right.mas_equalTo(-10);
            make.height.mas_equalTo(15);
        }];
        _diaryTitleLable.font = [UIFont systemFontOfSize:15];
        _diaryTitleLable.textColor = [UIColor grayColor];
    }
}

- (void)loadGroupIconImage{
    _contentImageView = [UIImageView new];
    [_cellContentView addSubview: _contentImageView];
    [_contentImageView mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.top.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
}

- (void)loadGroupNameLable{
    _groupNameLable = [UILabel new];
    [_cellContentView addSubview: _groupNameLable];
    [_groupNameLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(self.contentImageView.mas_right).offset(10);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
}

- (void)loadGroupIntorduceLable{
    _groupIntorduceLable = [UILabel new];
    [_cellContentView addSubview: _groupIntorduceLable];
    [_groupIntorduceLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(self.groupNameLable.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentImageView.mas_right).offset(10);
        make.right.mas_equalTo(-20);
    }];
    _groupIntorduceLable.numberOfLines = 1;
    _groupIntorduceLable.textColor = [UIColor grayColor];
    _groupIntorduceLable.font = [UIFont systemFontOfSize:12];
}

- (void) loadLearnRecordDateLable{
    _learnRecordDateLable = [UILabel new];
    [_cellContentView addSubview: _learnRecordDateLable];
    [_learnRecordDateLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(100);
    }];
}

- (UIView*) loadLearnRecordView:(NSString*) name :(NSString*) time{
    UIView *LearnRecordView = [UIView new];

    UILabel *nameLable = [UILabel new];
    [LearnRecordView addSubview:nameLable];
    [nameLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(150);
    }];
    nameLable.numberOfLines = 1;
    nameLable.text = name;
    
    UILabel *timeLable = [UILabel new];
    [LearnRecordView addSubview: timeLable];
    [timeLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.right.bottom.mas_equalTo(0);
        make.width.mas_equalTo(100);
    }];
    timeLable.numberOfLines = 1;
    timeLable.textAlignment = NSTextAlignmentRight;
    timeLable.text = time;
    return LearnRecordView;
}

- (void) attentionButtonClick{

}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
